<?php
/**
 * Uploads product images
 */
include_once("../config.inc.php");
include_once (CMS_ROOT . 'content/lib/cms_loader.class.php');
$iCount = 0;
$oProductModel = Loader::loadModel('newProduct');
$oDBInstance = Mysql_dbconnection::getInstance();
$oThumbCreator = new Thumb(IMAGE_UPLOAD_PATH, THUMBNAIL_PATH);
//Loads product ID from product model
$iID = $oProductModel->getProductID();
if (!isset($_FILES['datei1'])|| $_FILES['datei1']['tmp_name']==""){
    $oDBInstance->query("UPDATE `bs_product` SET mainimage =". DEFAULT_PRODUCT_IMAGE_ID." WHERE id=$iID; ");
}else{

    foreach ($_FILES as $aDatei2){

        $iCount++;
        if (isset($aDatei2['tmp_name'])&& $aDatei2['tmp_name']!=""){
            $aDateityp = GetImageSize($aDatei2['tmp_name']);
            //File type has to be gif or jpeg
            if($aDateityp[2] != 0){
                //Picture size have to be smaller then 1024 kb
                if($aDatei2['size'] <  1024000){
                    $sFile = IMAGE_UPLOAD_PATH.$aDatei2['name'];
                    move_uploaded_file($aDatei2['tmp_name'], $sFile);
                    $oThumbCreator->generateThumbFile(true,$aDatei2['name'], $aDatei2['name'], 95, $aDateityp[2], THUMB_MAX_X, THUMB_MAX_Y);
                }else{
                    echo "Das Bild darf nicht größer als 1000 kb sein ";
                }

            }else{
                echo "Bitte nur Bilder im Gif bzw. jpg Format hochladen";
            }


            //$oDBInstance->_setDefaultTable("bs_image");
            $sDateiname =$aDatei2['name'];
            $oDBInstance->query("INSERT INTO `bs_image` (
			`id` ,
			`link` ,
			`product`
			)VALUES (NULL , '$sDateiname', '$iID');");
            if ($iCount ==1){
                $oDBInstance->query("UPDATE `bs_product` SET mainimage = (SELECT MAX(id) FROM `bs_image`) WHERE id=$iID; ");

            }
        }
    }
}
    //Stores the category ID
    $iCategory = $oDBInstance->getSQLValueString($_GET['ca'], 'int');

    header("Location: index.php?co=product&ca=$iCategory");



    function __autoload($sClassName){
        Loader::loadClass($sClassName);
    }

    ?>